//
//  HMStatusPictureView.swift
//  Weibo-swift
//
//  Created by GD on 16/9/25.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit

private let hmStatusPictureView = "HMStatusPictureView"

let  itemMargin :CGFloat = 5

let itemWidth = (ScreenWidth - 2 * HMHomeTableViewCellMargin - 2 * itemMargin)/3

class HMStatusPictureView: UICollectionView {
    
    //绑定数据 模型
    var picUrls : [HMStatusPictureInfo]?{
        didSet{
            
            guard let urls = picUrls else {
                return
            }
            
            if urls.count > 0 {
                //计算
            }
            
            messageLabel.text = "\(picUrls?.count ?? 0)"
            
            let size = calcSize(count: picUrls?.count ?? 0)
            //使用make也可以.update???  巧妙的是,约束可以在不同的地方设置.  最后一个控件底部?  图片大小?
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            self.reloadData()
        }
    }
    
    
    //懒加载控件
    private lazy var messageLabel : UILabel = {
        let label = UILabel(textColor: UIColor.orange, fontSize: 14)
        
        return label
    }()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //创建布局对象
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        backgroundColor = UIColor.brown
        self.dataSource = self
        
        register(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: hmStatusPictureView)
        
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
    }
    
    
    //根据图片张数 计算图片大小
    private func calcSize(count: Int) -> CGSize{
//        let itemMargin :CGFloat = 5
//        
//        let itemWidth = (ScreenWidth - 2 * HMHomeTableViewCellMargin - 2 * itemMargin)/3
        //列数
        let cols = count > 3 ? 3 : count//count % 3 直接对3 取余? 感觉此处 count % 3 是一个效果
        //行数
        let rows = (count - 1) / 3 + 1 // 3张图片为例
        
        let currentWidth = itemWidth * CGFloat(cols) + CGFloat((cols - 1)) * itemMargin
        //当前宽度
        let  currentHeight = itemWidth * CGFloat(rows) + CGFloat((rows - 1)) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
    }
    
    
    

    
}

//
extension HMStatusPictureView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCell(withReuseIdentifier: hmStatusPictureView, for: indexPath) as! HMStatusPictureViewCell
        
        cell.backgroundColor = RandomColor()
        cell.statusPictureInfo = picUrls?[indexPath.item];
        
        return cell
    }
    
}


//自定义cell------该类cell只在本类当中使用. 其他地方不会用到.就可用这种方式定义自定义cell或其他类
class HMStatusPictureViewCell : UICollectionViewCell{
    
    var  statusPictureInfo : HMStatusPictureInfo?{
        didSet{
            if let url = statusPictureInfo?.thumbnail_pic{
                imageView.sd_setImage(with: URL(string:url), placeholderImage: UIImage (named: "timeline_image_placeholder"))
            }
        }
    }
    
    
    
    private lazy var imageView : UIImageView = {
        let view = UIImageView()
        view.image = UIImage (named: "timeline_image_placeholder")
        view.contentMode = .scaleAspectFill // 等比例 原样显示
        view.clipsToBounds = true
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsets.zero)
        }
    }
}
